
#include <iostream>
#include <fstream>
#include "lklist.h"

using namespace std;

typedef int elemtype;
typedef lklisttpl<elemtype> lklist;

void insert( lklist *&, int, elemtype );
void insert2( lklist *, int, elemtype );

int main()
{
    ifstream in("in.txt");
    vector<elemtype> v;
    lklist *h, *h2;
    int i, n;
    int t;

    in >> n;
    for ( i = 0; i < n; i++ ){
	in >> t;
	v.push_back(t);
    }

    h = createlk<lklist,elemtype>( v );
    printlk( h ); cout << endl;
    insert( h, 1, 10 );
    printlk( h ); cout << endl;
    destorylk( h );

    return 0;
}

/* ******************************************************
 *             Answer to this question.
 * ******************************************************/

void insert( lklist *&l, int i, elemtype b )
{
    lklist **p, *q;
    int j;
	
    assert( i > 0 );
    for( p = &l, j = 1; j < i && *p; j++, p = &(*p)->next )
	;
    assert( j == i );
	
    q = new lklist;
    q->data = b;
    q->next = *p;
    *p = q;
	
    return;
}


void insert2( lklist *l, int i, elemtype b )
{
    lklist *p, *q;
    int j;
	
    assert( i > 0 );
    for( p = l, j = 1; j < i && p->next; j++, p = p->next );
    assert( j == i );
	
    q = new lklist;
    q->data = b;
    q->next = p->next;
    p->next = q;
	
    return;
}
